﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.Extensions;
using ERPStore.Web.Routing;
using ERPStore.Offers.Repositories;

namespace ERPStore.Web.Html
{
	public static class OffersExtensions
	{
		public static MvcHtmlString ShowProductList(this HtmlHelper helper, string offerName, string viewName, int productCount, List<ERPStore.Models.SortProperty> sort = null)
		{
			return helper.Action<ERPStore.Offers.Controllers.OffersController>(i => i.ShowProductListView(offerName, viewName, productCount, sort));
		}

		public static MvcHtmlString ShowOffersByProductCategory(this HtmlHelper helper, string categoryName, int categoryId, string viewName, int? productCount)
		{
			return helper.Action<ERPStore.Offers.Controllers.OffersController>(i => i.ShowOffersByProductCategoryView(categoryName, categoryId, viewName, productCount));
		}

		public static MvcHtmlString ShowOffersByBrand(this HtmlHelper helper, string categoryName, int brandId, string viewName, int? productCount)
		{
			return helper.Action<ERPStore.Offers.Controllers.OffersController>(i => i.ShowOffersByBrandView(categoryName, brandId, viewName, productCount));
		}

		public static string OfferHref(this UrlHelper helper, string offerCode)
		{
			string routeName = null;
			var offerId = System.Text.RegularExpressions.Regex.Replace(offerCode, @"\D+", "");
			var id = 0;
			int.TryParse(offerId, out id);
			routeName = string.Format("OfferLink.{0}", id);
			return helper.RouteERPStoreUrl(routeName);
		}

		public static string GetOfferCSSTag(this ERPStore.Models.Product product, string prefix=null)
		{
			if (product.SelectedOffer != null)
			{
				var result = string.Format("offer{0}{1}", prefix, product.SelectedOffer.CSSTag);
				return result;
			}
			return null;
		}

		public static MvcHtmlString ShowProductCategoryListByOffer(this HtmlHelper helper, int offerId, string viewName)
		{
			return helper.Action<ERPStore.Offers.Controllers.OffersController>(m => m.ShowProductCategoryList(offerId, viewName));
		}

		public static MvcHtmlString ShowBrandListByOffer(this HtmlHelper helper, int offerId, string viewName)
		{
			return helper.Action<ERPStore.Offers.Controllers.OffersController>(m => m.ShowBrandList(offerId, viewName));
		}

	}
}
